﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Text;
using System.Collections;

using CE.Global;
using CE.User;
using CE.Database;

public partial class management_User_search_user : System.Web.UI.Page
{
    string PageCode = "CXTHRSUR";
    public bool _CountStatus = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            lblHeader.Visible = false;
            lblMessage.Visible = false;
            grvQuotesCount.Visible = false;

            lblProposalHeader.Visible = false;
            lblProposalMessage.Visible = false;
            grvProposalCount.Visible = false;
            hplView.Visible = false;

            txtEmail.Text = "";
            lblRecordCount.Text = "";
            txtCEID.Text = "";

            if (Request.QueryString["ceid"] != null)
            {
                trSearch.Visible = false;
                trReject.Visible = false;
                txtUserId.Text = Request.QueryString["ceid"].ToString();
                ddlUserType.Text = "400";
                Button1_Click(this, e);
            }
            else
            {
                trReject.Visible = true;
                trSearch.Visible = true;
                txtUserId.Text = "0";
            }
        }
        if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
        {
            CheckPageAccess();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        lblHeader.Visible = false;
        lblMessage.Visible = false;
        grvQuotesCount.Visible = false;

        lblProposalHeader.Visible = false;
        lblProposalMessage.Visible = false;
        grvProposalCount.Visible = false;
        hplView.Visible = false;

        MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
        string _spName = "sprocReportCXTHSearchUserSelectItem";

        if (Request.QueryString["ceid"] == null)
        {
            txtUserId.Text = "0";
            chkReject.Checked = false;
        }
        SortedList _spara = new SortedList();
        _spara.Add("@CEID", Convert.ToInt64(txtUserId.Text));
        _spara.Add("@UserTypeId", Convert.ToInt32(ddlUserType.Text.Trim()));
        txtUserTypeId.Text = ddlUserType.Text.Trim();
        _spara.Add("@Email", txtEmail.Text.Trim());
        DataSet _dsSummary = new DataSet();
        _dsSummary = _db.GetDataSet(_spName, _spara);
        if (_dsSummary != null && _dsSummary.Tables.Count > 0)
        {
            if (_dsSummary.Tables[0].Rows.Count > 0)
            {
                txtUserId.Text = _dsSummary.Tables[0].Rows[0]["CEID"].ToString();
                grvUser.DataSource = _dsSummary;
                grvUser.DataBind();
            }
            else
            {
                _dsSummary = null;
                txtUserId.Text = "0";
                grvUser.DataSource = _dsSummary;
                grvUser.DataBind();
            }
        }
        else
        {
            _dsSummary = null;
            grvUser.DataSource = _dsSummary;
            grvUser.DataBind();
        }
        lblRecordCount.Text = grvUser.Rows.Count.ToString() + " record(s).";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        txtEmail.Text = "";
        grvUser.DataSource = null;
        grvUser.DataBind();
        lblRecordCount.Text = "";
        txtUserId.Text = "0";
    }

    public string GetViewURL1(string UserId)
    {
        string _url = "";
        string _refId = CE.Security.MySecurity.Encrypt(UserId.ToString(), true);
        _url = "javascript:OpenProposal('" + UserId.ToString() + "');";
        return _url;
    }

    public string GetViewURL(string InsType, string UserId)
    {
        string _url = "#";
        //if (InsType.ToLower() == "Car Insurance")
        //{
        //    _url = "~/user/carquotehistory_report.aspx?ceid=" + UserId + "&menu=0";
        //}
        //else if (InsType.ToLower() == "Personal Accident")
        //{
        //    _url = "~/PersonalAccident/quotes_report.aspx?ceid=" + UserId + "&menu=0";
        //}
        //else if (InsType.ToLower() == "Travel Insurance")
        //{
        //    _url = "~/Travel_Insurance/quotes_report.aspx?ceid=" + UserId + "&menu=0";
        //}
        return _url;
    }

    protected void grvUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblHeader.Visible = true;
        GetQuotesCount();
        lblProposalHeader.Visible = true;
        GetProposalCount();
    }

    private void GetQuotesCount()
    {
        if (grvUser.Rows.Count > 0)
        {
            if (grvUser.SelectedIndex >= 0)
            {
                try
                {
                    if (grvUser.SelectedRow.Cells[1].Text.ToString().ToLower() == "customer")
                    {
                        lblMessage.Visible = false;
                        UserSummaryDB _usersummarydb = new UserSummaryDB();
                        DataSet _dsQuotes = new DataSet();
                        _dsQuotes = _usersummarydb.GetUserQuotesCount(Convert.ToInt64(grvUser.SelectedRow.Cells[0].Text));
                        if (_dsQuotes.Tables[0].Rows.Count > 0)
                        {
                            grvQuotesCount.Visible = true;
                            grvQuotesCount.DataSource = _dsQuotes;
                            grvQuotesCount.DataBind();
                        }
                    }
                    else
                    {
                        lblMessage.Visible = true;
                        grvQuotesCount.Visible = false;
                    }
                }
                catch (Exception Ex)
                {
                    Ex = null;
                    if (grvUser.Rows.Count > 0)
                    {
                        grvUser.SelectedIndex = 0;
                        GetQuotesCount();
                        lblMessage.Visible = false;
                        grvQuotesCount.Visible = false;
                    }
                }
            }
        }
    }
    
    protected void grvQuotesCount_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvQuotesCount.Rows.Count > 0)
        {
            if (grvQuotesCount.SelectedIndex >= 0)
            {
                Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());
                string domainUrl = "http://" + myUri.Authority + myUri.Segments[0] + myUri.Segments[1] + myUri.Segments[2];

                string _URL = "#";
                StringBuilder sb = new StringBuilder();
                
                if (_URL != "")
                {
                    String clientScriptName = "ButtonClickScript";
                    Type clientScriptType = this.GetType();
                    ClientScriptManager clientScript = Page.ClientScript;
                    if (!clientScript.IsClientScriptBlockRegistered(clientScriptType, clientScriptName))
                    {
                        sb.Append("<script type='text/javascript'>");
                        sb.Append("window.open(' " + _URL + "')"); //URL = where you want to redirect.
                        sb.Append("</script>");
                        clientScript.RegisterClientScriptBlock(clientScriptType, clientScriptName, sb.ToString());
                    }

                }
            }
        }
    }

    protected void grvQuotesCount_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    ImageButton ibtnView = new ImageButton();
        //    ibtnView = (ImageButton)e.Row.FindControl("ibtnView");
        //    ibtnView.Enabled = true;
        //    if (DataBinder.Eval(e.Row.DataItem, "QuotesCount").ToString() == "0")
        //    {
        //        ibtnView.Enabled = false;
        //    }
    }

    private void GetProposalCount()
    {
        if (grvUser.Rows.Count > 0)
        {
            if (grvUser.SelectedIndex >= 0)
            {
                try
                {
                    if (grvUser.SelectedRow.Cells[1].Text.ToString().ToLower() == "customer")
                    {
                        lblProposalMessage.Visible = false;
                        UserSummaryDB _usersummarydb = new UserSummaryDB();
                        DataSet _dsProposal = new DataSet();
                        _dsProposal = _usersummarydb.GetProposalCount(Convert.ToInt64(grvUser.SelectedRow.Cells[0].Text));
                        if (_dsProposal.Tables[0].Rows.Count > 0)
                        {
                            grvProposalCount.Visible = true;
                            grvProposalCount.DataSource = _dsProposal;
                            grvProposalCount.DataBind();
                        }
                    }
                    else
                    {
                        lblProposalMessage.Visible = true;
                        grvProposalCount.Visible = false;
                        hplView.Visible = false;
                    }
                }
                catch (Exception Ex)
                {
                    Ex = null;
                    if (grvUser.Rows.Count > 0)
                    {
                        grvUser.SelectedIndex = 0;
                        GetProposalCount();
                        lblProposalMessage.Visible = false;
                        grvProposalCount.Visible = false;
                        hplView.Visible = false;
                    }
                }
            }
        }
    }

    protected void grvProposalCount_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            txtCEID.Text = DataBinder.Eval(e.Row.DataItem, "RefCEUserId").ToString();
            if (DataBinder.Eval(e.Row.DataItem, "ProposalCount").ToString() == "0")
            {
                if (_CountStatus == true)
                {
                    _CountStatus = true;
                }
            }
            else
            {
                _CountStatus = true;
            }
            if (_CountStatus == true)
            {
                hplView.NavigateUrl = "~/proposal_report.aspx?ceid=" + txtCEID.Text.Trim() + "&menu=0&instype=All";
                //hplView.Visible = true;
            }
        }
    }

    protected void btnReject_Click(object sender, EventArgs e)
    {
        if (txtUserId.Text.Length > 0 && txtUserId.Text != "0")
        {
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            string strQuery = "Update tblCEUser Set IsReject = " + (chkReject.Checked ? "1" : "0") + " Where CEID = " + txtUserId.Text + " And UsrTypeId = " + txtUserTypeId.Text;
            _db.ExecuteNonSqlStatement(strQuery);
            Button1_Click(sender, e);
        }

    }

    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Logout.aspx");
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else if(PageAccess > 0)
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {        
        if (PageAccess == 100)
        {
            btnReject.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnReject.Enabled = true;
        }
    }
    
}
